<?php

/**
 * This is the model class for table "taxonomy_vocabulary".
 */
require_once(dirname(__FILE__) . '/base/VocabularyBase.php');
class Vocabulary extends VocabularyBase
{
    const STATE_ACTIVE = 1, STATE_DEACTIVE = 2;

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            'terms' => array(self::HAS_MANY, 'Term', 'v_id'),
        );
    }

    public function getStateOptions()
    {
        return array(
            self::STATE_ACTIVE => 'Active',
            self::STATE_DEACTIVE => 'Deactive',
        );
    }

    public function getStateText($id = null)
    {
        if (is_null($id))
            $id = $this->state;

        $options = $this->getStateOptions();
        return isset($options[$this->state]) ? $options[$this->state] : '';
    }

    public function beforeValidate()
    {
        if (empty($this->module))
            $this->module = 'system';
        return parent::beforeValidate();
    }

    public function getPropertyClassName()
    {
        $data = $this->getPropertyInfo();
        return $data[1];
    }

    public function getPropertyModule()
    {
        $data = $this->getPropertyInfo();
        return $data[0];
    }

    public function getPropertyInfo()
    {
        static $module, $className;
        if (isset($module, $className))
            return array($module, $className);
        $module = '';
        $className = '';
        $criteria = new CDbCriteria();
        $criteria->with = array('contentType');
        $form = Form::model()->findByPk($this->properties_form_id, $criteria);
        if (is_object($form) && is_object($form->contentType)) {
            list($module, $path, $className) = explode('.', $form->contentType->model);
            Yii::import($form->contentType->model);
        }
        return array($module, $className);
    }
}